<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%
	String idUser=request.getParameter("user_id");
int user_id = Integer.parseInt(idUser);
%>
<%@ page import="servlets.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<%
	SessionFactory sessionFactory = HibernateUtil
						.getSessionAnnotationFactory();
				Session s = sessionFactory.openSession();
				s.beginTransaction();
				UserMethods um= new UserMethods();
				RelationMethods rm = new RelationMethods();
				InvitationMethods im= new InvitationMethods();
				
				response.setContentType("text/html");
				
					Integer user = (Integer) session.getAttribute("user_id");
					User u = um.id_to_user(sessionFactory, s, user_id);
%>
<div class="page-header">
	<h2 class="page-title">
		Profil uzytkownika -
		<%=u.getUsername()%></h2>
</div>
<div id="content" style="margin-left: -4%;">
	<br />


	<div class="col-sm-4 col-md-4 user-details">
		<div class="user-image">
			<img src="main/userAvatar.jsp?user_id=<%=user_id%>" alt="UserAvatar"
				class="img-circle" style="height: 115px">
		</div>
		<div class="user-info-block">
			<div class="user-heading">
				<h3><%=u.getUsername()%></h3>
				<span class="help-block"> <%
 	if(user !=null){
                     	if (um.id_to_user(sessionFactory, s, user).getType() == "client"){
                    
                             if (rm.listAllFriends(sessionFactory, s, user).contains(u)){
 %>
					<input type="submit" class="mainBtn" value="Usuń z grona znajomych" />
					<%
						} else  {
					                      List <Invitation> wyslane =  im.listActiveInvitationsBySender(sessionFactory, s, user);
					                      int i=0;
					                      for (Invitation w : wyslane ){
					                    	  if (w.getInvited()== u.getUser_id()) i++;
					                      }
					                      
					                      if (i==0){
					                         
					                          if(user_id != (Integer) session.getAttribute("user_id")){
					%><input type="submit" class="mainBtn"
					value="Wyslij zaproszenie do grona znajomych" /> <%
 	}}
                       else if (i==1){
 %> <input type="submit" class="mainBtn"
					value="Usuń zaproszenie do grona znajomych" /> <%
 	}   
                           }
                     	}
                     }
 %></span>
			</div>
			<ul class="navigation">
				<li class="active"><a data-toggle="tab" href="#information">
						<span class="fa fa-user"> </span>
				</a></li>
				<%
					if("admin".equals(um.id_to_user(sessionFactory, s, user).getType()) || um.id_to_user(sessionFactory, s, user) == u){
				%>

				<li><a data-toggle="tab" href="#settings"> <span
						class="fa fa-cog"></span>
				</a></li>
				<%
					}
				%>

				<%
					if (um.id_to_user(sessionFactory, s, user_id) != um.id_to_user(sessionFactory, s, user)){
				%>
				<li><a data-toggle="tab" href="#email"> <span
						class="fa fa-envelope"></span>
				</a></li>
				<%
					}
				%>
				<% if("client".equals(u.getType())){ %>
				<li><a data-toggle="tab" href="#events"> <span
						class="fa fa-calendar"></span>
				</a></li>
				<%} %>
			</ul>
			<div class="user-body">
				<div class="tab-content">
					<div id="information" class="tab-pane active">
						<h4>Informacje o uzytkowniku</h4>
						<%
							if ("client".equals(um.id_to_user(sessionFactory, s, user).getType())){
						%>
						<br>
						
						<u>Z nami od:</u>
						<%=u.getAdd_date()%>
						<br>
						
						<u> Wiek:</u>
						<br>
						<u> Typ:</u>
						<%if("client".equals(u.getType())) out.println("gracz");
						else if ("worker".equals(u.getType())) out.println("pracownik");
						else if("admin".equals(u.getType())) out.println("administrator");%>
						
						<br> <u>Plec:</u>
						<%
							if (um.id_to_user(sessionFactory, s, user_id).getGender()== "female"){
						                            	out.println("kobieta"); }
						                            	else
						                            	out.println("mezczyzna");
						%>
						

						<%
							}	
						                            
						                            if("worker".equals(um.id_to_user(sessionFactory, s, user).getType()) || "admin".equals(um.id_to_user(sessionFactory, s, user).getType())){
						%>
						<u>Adres e-mail:</u>
						<%=u.getEmail()%>
						<br>
						<u> Imie:</u>
						<%=u.getName() %>
						<br>
						<u> Nazwisko:</u>
						<%=u.getSurname()%>
						<br>
						<u>Numer telefonu:</u>
						<%=u.getPhone() %>
<br>

						<%
							}
						%>
					</div>



					<div id="settings" class="tab-pane">
						<h4>Ustawienia profilu</h4>
						<% if ("admin".equals(u.getType())){
							%>
							<form action="../EditUser" method="POST" name='formularz' enctype="multipart/form-data">

		<table>
			<tr>
				<td>Nick:</td>
				<td>
				<input type="hidden" name="user_id" id="user_id" value="<%=u.getUser_id()%>">
					<%
						out.println(u.getUsername());
					%>
				</td>

			</tr>
			<tr>

				<td>Data urodzenia:</td>
				<td><input type="date" id="birth_date" name="birth_date"
					placeholder="<%=u.getBirthdate()%>" value="<%=u.getBirthdate()%>"
					pattern="(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))">
				</td>

			</tr>

			<tr>

				<td>Podaj email:</td>
				<td><input type="email" name="email"
					placeholder="<%out.println(u.getEmail());%>" maxlength="255"
					pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"></td>

			</tr>

			<tr>
				<td>Hasło:</td>
				<td><input type="password" id="password" name="password"
					 maxlength="80"></td>

			</tr>
			<tr>
				<td>Powtórz haslo:</td>
				<td><input type="password" id="confirm_password" 
					name="confirm_password" maxlength="80"></td>

			</tr>
			<tr>
				<td>Avatar:</td>
				<td><input type="file" name="avatar" id="avatar"></td>
				<td> <img src="main/userAvatar.jsp?user_id=<%=(Integer) session.getAttribute("user_id") %>" alt="UserAvatar" class="img-circle" style="height: 115px">
				<br>
				Aktualny avatar
				</td>
				
			</tr>
			<tr>
				<td><input type="hidden" id="type" name="type" value="client" />
				</td>
			</tr>
			<tr>
				<td>Płec:</td>
				<td><select id="gender" name="gender">
						<%
							if (u.getGender() == "male") {
						%>
						<option value="male" selected>męzczyzna</option>
						<option value="female">kobieta</option>

						<%
							} else {
						%>
						<option value="male">męzczyzna</option>
						<option value="female" selected>kobieta</option>
						<%
							}
						%>
				</select></td>

			</tr>
			<tr>
				<td>Imię: *</td>
				<td><input type="text" name="name" id="name"
					placeholder="<%out.println(u.getName());%>" maxlength="255"></td>

			</tr>
			<tr>
				<td>Nazwisko: *</td>
				<td><input type="text" name="surname" id="surname"
					placeholder="<%String surname = u.getSurname();
				if (surname != null)
					out.println(surname);%>"
					maxlength="255"></td>

			</tr>
			<tr>
				<td>Numer telefonu: *</td>
				<td><input type="tel" name="phone" id="phone"
					placeholder="<%String phone = u.getPhone();
				if (phone != null)
					out.println(phone);%>"
					maxlength="80" pattern="\d{3}[\-]\d{3}[\-]\d{3}"></td>

			</tr>

		</table>
		<input type="submit" class="mainBtn" value="Zmień dane"> <br />
	</form>

							
							
							
							
							<%} 
							if (u.equals(um.id_to_user(sessionFactory, s, user))){
								if("client".equals(u.getType())){
							%>
					<a href="http://localhost:8080/Thesis/social/index.jsp?page=settings&group=edit">Przejdz do edycji danych</a>
					<%} 
								else if ("management".equals(u.getType())){%>
								<a href="http://localhost:8080/Thesis/management/index.jsp?page=settings&group=edit">Przejdz do edycji danych</a>
				<%	}
								else if("admin".equals(u.getType())){%>
											<a href="http://localhost:8080/Thesis/management/index.jsp?page=settings&group=edit">Przejdz do edycji danych</a>
					<%	}%>

<%} %>
					</div>
					<div id="email" class="tab-pane">
						<h4>Nowa wiadomosc</h4>
						<br />
						<%
							if (user_id == (Integer) session.getAttribute("user_id")){
						                        	out.println("Nie mozesz wyslac wiadomosci do samego siebie");
						                        	}
						                        	else {
						%>

						<form action="../MessageSend" method="POST" name='formularz'>
							<input type="hidden" id="sender_id" name="sender_id"
								value="<%=session.getAttribute("user_id")%>" /> <input
								type="hidden" id="receiver" name="receiver"
								value="<%=request.getParameter("user_id")%>" />
							<table>
								<tr>
									<td>Temat wiadomości:</td>
									<td><input type="text" id="subject" name="subject"
										style="width: 80%" /></td>
								</tr>
								<tr>
									<td>Treśc wiadomości:</td>
									<td><textarea id="content" name="content" cols="50"
											rows="10" style="width: 80%"> </textarea></td>
								</tr>
								<tr>
									<td><input type="submit" class="mainBtn"
										value="Wyślij wiadomość" /></td>
								</tr>
							</table>
						</form>
						<%
							}
						%>
					</div>
					<div id="events" class="tab-pane">

						<script>
							$("a.locals").click(
									function() {
										$("#friends_content").hide().fadeIn(
												1000).load(
												"main/localProfile.jsp?local_id="
														+ $(this).attr('id'));
										return false;
									});

							$("a.events").click(
									function() {
										$("#friends_content").hide().fadeIn(
												1000).load(
												"events/eventProfile.jsp?event_id="
														+ $(this).attr('id'));
										return false;
									});
						</script>

						<%
							EventMethods em = new EventMethods();
							response.setContentType("text/html");
							LocalMethods lm = new LocalMethods();

							List<EventParticipating> eplist = em.listParticipatingByUser(
									sessionFactory, s,
									Integer.parseInt((String) request.getParameter("user_id")));
						%>
						<%
							if (eplist.size() > 0) {
						%>
						<h4>Udział w wydarzeniach:</h4>
						<table class="table">

							<thead>
								<tr>
									<td>Nazwa wydarzenia</td>
									<td>Lokal</td>
									<td>Poczatek</td>
									<td>Koniec</td>
									<td>Status</td>
								</tr>
							</thead>
							<%
								for (EventParticipating ep1 : eplist) {
										if (em.findUserEventById(sessionFactory, s,
												ep1.getEvent_id()) == null
												|| em.findUserEventById(sessionFactory, s,
														ep1.getEvent_id()).isEmpty()) {
											out.println("Uzytkownik nie bierze jeszcze udzialu w zadnym wydarzeniu");
											System.out.println("PUSTO");
										} else {
							%>

							<%
								UserEvent ue = em.findUserEventById(sessionFactory, s,
													ep1.getEvent_id()).get(0);
											String nazwa = ue.getEventname();
											int lokal_id = ue.getLocal_id();
											Local l = lm.findLocalById(sessionFactory, s, lokal_id);
											String localName = l.getCountry() + ", " + l.getCity()
													+ ", " + l.getStreet() + " "
													+ l.getLocal_number();
											String poczatek = ue.getStart();
											String koniec = ue.getEnd();
											Boolean status = ep1.getMaybe();
											String stat = null;
											if (status == true)
												stat = "Moze";
											else
												stat = "Na pewno";
							%>
							<tbody>
								<tr>
									<td>
										<%
											out.println("<a class=\"events\" id=\""
																+ ep1.getEvent_id() + "\" href=\"#\">" + nazwa
																+ "</a>");
										%>
									</td>
									<td>
										<%
											out.println("<a class=\"locals\" id=\""
																+ ue.getLocal_id() + "\" href=\"#\">"
																+ localName + "</a>");
										%>
									</td>
									<td>
										<%
											out.println(poczatek);
										%>
									</td>
									<td>
										<%
											out.println(koniec);
										%>
									</td>
									<td>
										<%
											out.println(stat);
										%>
									
								</tr>
							</tbody>
							<%
								}
									}
								} else
									out.println("<h4>Uzytkownik nie bierze udzialu jeszcze w zadnym wydarzeniu</h4>");

								s.getTransaction().commit();
							%>




						</table>
					</div>
				</div>
			</div>
		</div>
	</div>


</div>
<style>
.user-details {
	position: relative;
	padding: 0;
	width: 100%
}

.user-details .user-image {
	position: relative;
	z-index: 1;
	width: 100%;
	text-align: center;
}

.user-image img {
	clear: both;
	margin: auto;
	position: relative;
}

.user-details .user-info-block {
	width: 100%;
	position: absolute;
	top: 55px;
	background-color: rgba(0, 0, 0, 0.7);
	z-index: 0;
	padding-top: 35px;
	width: 100%;
}

.user-info-block .user-heading {
	width: 100%;
	text-align: center;
}

.user-info-block .navigation {
	float: left;
	width: 100%;
	margin: 0;
	padding: 0;
	list-style: none;
	border-bottom: 1px solid #428BCA;
	border-top: 1px solid #428BCA;
}

.navigation li {
	float: left;
	margin: 0;
	padding: 0;
}

.navigation li a {
	padding: 20px 30px;
	float: left;
}

.navigation li.active a {
	background: #428BCA;
	color: #fff;
}

.user-info-block .user-body {
	float: left;
	width: 90%;
}

.user-body .tab-content>div {
	float: left;
	width: 100%;
	color: white
}

.user-body .tab-content h4 {
	width: 100%;
	margin: 10px 0;
	color: white;
}
</style>